/*   1:    */ package nl.siegmann.epublib.viewer;
/*   2:    */ 
/*   3:    */ import java.awt.BorderLayout;
/*   4:    */ import java.awt.Dimension;
/*   5:    */ import java.awt.event.ActionEvent;
/*   6:    */ import java.awt.event.ActionListener;
/*   7:    */ import java.io.File;
/*   8:    */ import java.io.FileInputStream;
/*   9:    */ import java.io.FileNotFoundException;
/*  10:    */ import java.io.FileOutputStream;
/*  11:    */ import java.io.IOException;
/*  12:    */ import java.io.InputStream;
/*  13:    */ import java.util.Collections;
/*  14:    */ import javax.swing.JFileChooser;
/*  15:    */ import javax.swing.JFrame;
/*  16:    */ import javax.swing.JMenu;
/*  17:    */ import javax.swing.JMenuBar;
/*  18:    */ import javax.swing.JMenuItem;
/*  19:    */ import javax.swing.JPanel;
/*  20:    */ import javax.swing.JSplitPane;
/*  21:    */ import javax.swing.KeyStroke;
/*  22:    */ import javax.swing.SwingUtilities;
/*  23:    */ import javax.swing.UIManager;
/*  24:    */ import javax.swing.filechooser.FileNameExtensionFilter;
/*  25:    */ import nl.siegmann.epublib.browsersupport.NavigationHistory;
/*  26:    */ import nl.siegmann.epublib.browsersupport.Navigator;
/*  27:    */ import nl.siegmann.epublib.domain.Book;
/*  28:    */ import nl.siegmann.epublib.epub.BookProcessorPipeline;
/*  29:    */ import nl.siegmann.epublib.epub.EpubReader;
/*  30:    */ import nl.siegmann.epublib.epub.EpubWriter;
/*  31:    */ import org.apache.commons.lang.StringUtils;
/*  32:    */ import org.slf4j.Logger;
/*  33:    */ import org.slf4j.LoggerFactory;
/*  34:    */ 
/*  35:    */ public class Viewer
/*  36:    */ {
/*  37:    */   private static final long serialVersionUID = 1610691708767665447L;
/*  38: 45 */   static final Logger log = LoggerFactory.getLogger(Viewer.class);
/*  39:    */   private final JFrame mainWindow;
/*  40:    */   private BrowseBar browseBar;
/*  41:    */   private JSplitPane mainSplitPane;
/*  42:    */   private JSplitPane leftSplitPane;
/*  43:    */   private JSplitPane rightSplitPane;
/*  44: 51 */   private Navigator navigator = new Navigator();
/*  45:    */   NavigationHistory browserHistory;
/*  46: 53 */   private BookProcessorPipeline epubCleaner = new BookProcessorPipeline(Collections.emptyList());
/*  47:    */   
/*  48:    */   public Viewer(InputStream bookStream)
/*  49:    */   {
/*  50: 56 */     this.mainWindow = createMainWindow();
/*  51:    */     try
/*  52:    */     {
/*  53: 59 */       Book book = new EpubReader().readEpub(bookStream);
/*  54: 60 */       gotoBook(book);
/*  55:    */     }
/*  56:    */     catch (IOException e)
/*  57:    */     {
/*  58: 62 */       log.error(e.getMessage(), e);
/*  59:    */     }
/*  60:    */   }
/*  61:    */   
/*  62:    */   public Viewer(Book book)
/*  63:    */   {
/*  64: 67 */     this.mainWindow = createMainWindow();
/*  65: 68 */     gotoBook(book);
/*  66:    */   }
/*  67:    */   
/*  68:    */   private JFrame createMainWindow()
/*  69:    */   {
/*  70: 72 */     JFrame result = new JFrame();
/*  71: 73 */     result.setDefaultCloseOperation(3);
/*  72:    */     
/*  73: 75 */     result.setJMenuBar(createMenuBar());
/*  74:    */     
/*  75: 77 */     JPanel mainPanel = new JPanel(new BorderLayout());
/*  76:    */     
/*  77: 79 */     this.leftSplitPane = new JSplitPane(0);
/*  78: 80 */     this.leftSplitPane.setTopComponent(new TableOfContentsPane(this.navigator));
/*  79: 81 */     this.leftSplitPane.setBottomComponent(new GuidePane(this.navigator));
/*  80: 82 */     this.leftSplitPane.setOneTouchExpandable(true);
/*  81: 83 */     this.leftSplitPane.setContinuousLayout(true);
/*  82: 84 */     this.leftSplitPane.setResizeWeight(0.8D);
/*  83:    */     
/*  84: 86 */     this.rightSplitPane = new JSplitPane(1);
/*  85: 87 */     this.rightSplitPane.setOneTouchExpandable(true);
/*  86: 88 */     this.rightSplitPane.setContinuousLayout(true);
/*  87: 89 */     this.rightSplitPane.setResizeWeight(1.0D);
/*  88: 90 */     ContentPane htmlPane = new ContentPane(this.navigator);
/*  89: 91 */     JPanel contentPanel = new JPanel(new BorderLayout());
/*  90: 92 */     contentPanel.add(htmlPane, "Center");
/*  91: 93 */     this.browseBar = new BrowseBar(this.navigator, htmlPane);
/*  92: 94 */     contentPanel.add(this.browseBar, "South");
/*  93: 95 */     this.rightSplitPane.setLeftComponent(contentPanel);
/*  94: 96 */     this.rightSplitPane.setRightComponent(new MetadataPane(this.navigator));
/*  95:    */     
/*  96: 98 */     this.mainSplitPane = new JSplitPane(1);
/*  97: 99 */     this.mainSplitPane.setLeftComponent(this.leftSplitPane);
/*  98:100 */     this.mainSplitPane.setRightComponent(this.rightSplitPane);
/*  99:101 */     this.mainSplitPane.setOneTouchExpandable(true);
/* 100:102 */     this.mainSplitPane.setContinuousLayout(true);
/* 101:103 */     this.mainSplitPane.setResizeWeight(0.0D);
/* 102:    */     
/* 103:105 */     mainPanel.add(this.mainSplitPane, "Center");
/* 104:106 */     mainPanel.setPreferredSize(new Dimension(1000, 750));
/* 105:107 */     mainPanel.add(new NavigationBar(this.navigator), "North");
/* 106:    */     
/* 107:109 */     result.add(mainPanel);
/* 108:110 */     result.pack();
/* 109:111 */     setLayout(Layout.TocContentMeta);
/* 110:112 */     result.setVisible(true);
/* 111:113 */     return result;
/* 112:    */   }
/* 113:    */   
/* 114:    */   private void gotoBook(Book book)
/* 115:    */   {
/* 116:117 */     this.mainWindow.setTitle(book.getTitle());
/* 117:118 */     this.navigator.gotoBook(book, this);
/* 118:    */   }
/* 119:    */   
/* 120:    */   private static String getText(String text)
/* 121:    */   {
/* 122:122 */     return text;
/* 123:    */   }
/* 124:    */   
/* 125:    */   private static JFileChooser createFileChooser(File startDir)
/* 126:    */   {
/* 127:126 */     if (startDir == null)
/* 128:    */     {
/* 129:127 */       startDir = new File(System.getProperty("user.home"));
/* 130:128 */       if (!startDir.exists()) {
/* 131:129 */         startDir = null;
/* 132:    */       }
/* 133:    */     }
/* 134:132 */     JFileChooser fileChooser = new JFileChooser(startDir);
/* 135:133 */     fileChooser.setAcceptAllFileFilterUsed(true);
/* 136:134 */     fileChooser.setFileFilter(new FileNameExtensionFilter("EPub files", new String[] { "epub" }));
/* 137:    */     
/* 138:136 */     return fileChooser;
/* 139:    */   }
/* 140:    */   
/* 141:    */   private JMenuBar createMenuBar()
/* 142:    */   {
/* 143:140 */     JMenuBar menuBar = new JMenuBar();
/* 144:141 */     JMenu fileMenu = new JMenu(getText("File"));
/* 145:142 */     menuBar.add(fileMenu);
/* 146:143 */     JMenuItem openFileMenuItem = new JMenuItem(getText("Open"));
/* 147:144 */     openFileMenuItem.setAccelerator(KeyStroke.getKeyStroke(79, 2));
/* 148:145 */     openFileMenuItem.addActionListener(new ActionListener()
/* 149:    */     {
/* 150:    */       private File previousDir;
/* 151:    */       
/* 152:    */       public void actionPerformed(ActionEvent e)
/* 153:    */       {
/* 154:150 */         JFileChooser fileChooser = Viewer.createFileChooser(this.previousDir);
/* 155:151 */         int returnVal = fileChooser.showOpenDialog(Viewer.this.mainWindow);
/* 156:152 */         if (returnVal != 0) {
/* 157:153 */           return;
/* 158:    */         }
/* 159:155 */         File selectedFile = fileChooser.getSelectedFile();
/* 160:156 */         if (selectedFile == null) {
/* 161:157 */           return;
/* 162:    */         }
/* 163:159 */         if (!selectedFile.isDirectory()) {
/* 164:160 */           this.previousDir = selectedFile.getParentFile();
/* 165:    */         }
/* 166:    */         try
/* 167:    */         {
/* 168:163 */           Book book = new EpubReader().readEpub(new FileInputStream(selectedFile));
/* 169:164 */           Viewer.this.gotoBook(book);
/* 170:    */         }
/* 171:    */         catch (Exception e1)
/* 172:    */         {
/* 173:166 */           Viewer.log.error(e1.getMessage(), e1);
/* 174:    */         }
/* 175:    */       }
/* 176:169 */     });
/* 177:170 */     fileMenu.add(openFileMenuItem);
/* 178:    */     
/* 179:172 */     JMenuItem saveFileMenuItem = new JMenuItem(getText("Save as ..."));
/* 180:173 */     saveFileMenuItem.setAccelerator(KeyStroke.getKeyStroke(83, 3));
/* 181:174 */     saveFileMenuItem.addActionListener(new ActionListener()
/* 182:    */     {
/* 183:    */       private File previousDir;
/* 184:    */       
/* 185:    */       public void actionPerformed(ActionEvent e)
/* 186:    */       {
/* 187:179 */         if (Viewer.this.navigator.getBook() == null) {
/* 188:180 */           return;
/* 189:    */         }
/* 190:182 */         JFileChooser fileChooser = Viewer.createFileChooser(this.previousDir);
/* 191:183 */         int returnVal = fileChooser.showOpenDialog(Viewer.this.mainWindow);
/* 192:184 */         if (returnVal != 0) {
/* 193:185 */           return;
/* 194:    */         }
/* 195:187 */         File selectedFile = fileChooser.getSelectedFile();
/* 196:188 */         if (selectedFile == null) {
/* 197:189 */           return;
/* 198:    */         }
/* 199:191 */         if (!selectedFile.isDirectory()) {
/* 200:192 */           this.previousDir = selectedFile.getParentFile();
/* 201:    */         }
/* 202:    */         try
/* 203:    */         {
/* 204:195 */           new EpubWriter().write(Viewer.this.navigator.getBook(), new FileOutputStream(selectedFile));
/* 205:    */         }
/* 206:    */         catch (Exception e1)
/* 207:    */         {
/* 208:197 */           Viewer.log.error(e1.getMessage(), e1);
/* 209:    */         }
/* 210:    */       }
/* 211:200 */     });
/* 212:201 */     fileMenu.add(saveFileMenuItem);
/* 213:    */     
/* 214:203 */     JMenuItem reloadMenuItem = new JMenuItem(getText("Reload"));
/* 215:204 */     reloadMenuItem.setAccelerator(KeyStroke.getKeyStroke(82, 2));
/* 216:205 */     reloadMenuItem.addActionListener(new ActionListener()
/* 217:    */     {
/* 218:    */       public void actionPerformed(ActionEvent e)
/* 219:    */       {
/* 220:208 */         Viewer.this.gotoBook(Viewer.this.navigator.getBook());
/* 221:    */       }
/* 222:210 */     });
/* 223:211 */     fileMenu.add(reloadMenuItem);
/* 224:    */     
/* 225:213 */     JMenuItem exitMenuItem = new JMenuItem(getText("Exit"));
/* 226:214 */     exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(81, 2));
/* 227:215 */     exitMenuItem.addActionListener(new ActionListener()
/* 228:    */     {
/* 229:    */       public void actionPerformed(ActionEvent e)
/* 230:    */       {
/* 231:218 */         System.exit(0);
/* 232:    */       }
/* 233:220 */     });
/* 234:221 */     fileMenu.add(exitMenuItem);
/* 235:    */     
/* 236:223 */     JMenu viewMenu = new JMenu(getText("View"));
/* 237:224 */     menuBar.add(viewMenu);
/* 238:    */     
/* 239:226 */     JMenuItem viewTocContentMenuItem = new JMenuItem(getText("TOCContent"), ViewerUtil.createImageIcon("layout-toc-content"));
/* 240:227 */     viewTocContentMenuItem.setAccelerator(KeyStroke.getKeyStroke(50, 2));
/* 241:228 */     viewTocContentMenuItem.addActionListener(new ActionListener()
/* 242:    */     {
/* 243:    */       public void actionPerformed(ActionEvent e)
/* 244:    */       {
/* 245:231 */         Viewer.this.setLayout(Viewer.Layout.TocContent);
/* 246:    */       }
/* 247:233 */     });
/* 248:234 */     viewMenu.add(viewTocContentMenuItem);
/* 249:    */     
/* 250:236 */     JMenuItem viewContentMenuItem = new JMenuItem(getText("Content"), ViewerUtil.createImageIcon("layout-content"));
/* 251:237 */     viewContentMenuItem.setAccelerator(KeyStroke.getKeyStroke(49, 2));
/* 252:238 */     viewContentMenuItem.addActionListener(new ActionListener()
/* 253:    */     {
/* 254:    */       public void actionPerformed(ActionEvent e)
/* 255:    */       {
/* 256:241 */         Viewer.this.setLayout(Viewer.Layout.Content);
/* 257:    */       }
/* 258:243 */     });
/* 259:244 */     viewMenu.add(viewContentMenuItem);
/* 260:    */     
/* 261:246 */     JMenuItem viewTocContentMetaMenuItem = new JMenuItem(getText("TocContentMeta"), ViewerUtil.createImageIcon("layout-toc-content-meta"));
/* 262:247 */     viewTocContentMetaMenuItem.setAccelerator(KeyStroke.getKeyStroke(51, 2));
/* 263:248 */     viewTocContentMetaMenuItem.addActionListener(new ActionListener()
/* 264:    */     {
/* 265:    */       public void actionPerformed(ActionEvent e)
/* 266:    */       {
/* 267:251 */         Viewer.this.setLayout(Viewer.Layout.TocContentMeta);
/* 268:    */       }
/* 269:253 */     });
/* 270:254 */     viewMenu.add(viewTocContentMetaMenuItem);
/* 271:    */     
/* 272:256 */     JMenu helpMenu = new JMenu(getText("Help"));
/* 273:257 */     menuBar.add(helpMenu);
/* 274:258 */     JMenuItem aboutMenuItem = new JMenuItem(getText("About"));
/* 275:259 */     aboutMenuItem.addActionListener(new ActionListener()
/* 276:    */     {
/* 277:    */       public void actionPerformed(ActionEvent e)
/* 278:    */       {
/* 279:262 */         new AboutDialog(Viewer.this.mainWindow);
/* 280:    */       }
/* 281:264 */     });
/* 282:265 */     helpMenu.add(aboutMenuItem);
/* 283:    */     
/* 284:267 */     return menuBar;
/* 285:    */   }
/* 286:    */   
/* 287:    */   private class LayoutX
/* 288:    */   {
/* 289:    */     private boolean tocPaneVisible;
/* 290:    */     private boolean contentPaneVisible;
/* 291:    */     private boolean metaPaneVisible;
/* 292:    */     
/* 293:    */     private LayoutX() {}
/* 294:    */   }
/* 295:    */   
/* 296:    */   private static enum Layout
/* 297:    */   {
/* 298:271 */     TocContentMeta,  TocContent,  Content;
/* 299:    */     
/* 300:    */     private Layout() {}
/* 301:    */   }
/* 302:    */   
/* 303:    */   private void setLayout(Layout layout)
/* 304:    */   {
/* 305:283 */     switch (10.$SwitchMap$nl$siegmann$epublib$viewer$Viewer$Layout[layout.ordinal()])
/* 306:    */     {
/* 307:    */     case 1: 
/* 308:285 */       this.mainSplitPane.setDividerLocation(0.0D);
/* 309:286 */       this.rightSplitPane.setDividerLocation(1.0D);
/* 310:287 */       break;
/* 311:    */     case 2: 
/* 312:289 */       this.mainSplitPane.setDividerLocation(0.2D);
/* 313:290 */       this.rightSplitPane.setDividerLocation(1.0D);
/* 314:291 */       break;
/* 315:    */     case 3: 
/* 316:293 */       this.mainSplitPane.setDividerLocation(0.2D);
/* 317:294 */       this.rightSplitPane.setDividerLocation(0.6D);
/* 318:    */     }
/* 319:    */   }
/* 320:    */   
/* 321:    */   private static InputStream getBookInputStream(String[] args)
/* 322:    */   {
/* 323:307 */     String bookFile = null;
/* 324:308 */     if (args.length > 0) {
/* 325:309 */       bookFile = args[0];
/* 326:    */     }
/* 327:311 */     InputStream result = null;
/* 328:312 */     if (!StringUtils.isBlank(bookFile)) {
/* 329:    */       try
/* 330:    */       {
/* 331:314 */         result = new FileInputStream(bookFile);
/* 332:    */       }
/* 333:    */       catch (Exception e)
/* 334:    */       {
/* 335:316 */         log.error("Unable to open " + bookFile, e);
/* 336:    */       }
/* 337:    */     }
/* 338:319 */     if (result == null) {
/* 339:320 */       result = Viewer.class.getResourceAsStream("/viewer/epublibviewer-help.epub");
/* 340:    */     }
/* 341:322 */     return result;
/* 342:    */   }
/* 343:    */   
/* 344:    */   public static void main(String[] args)
/* 345:    */     throws FileNotFoundException, IOException
/* 346:    */   {
/* 347:    */     try
/* 348:    */     {
/* 349:328 */       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
/* 350:    */     }
/* 351:    */     catch (Exception e)
/* 352:    */     {
/* 353:330 */       log.error("Unable to set native look and feel", e);
/* 354:    */     }
/* 355:333 */     InputStream bookStream = getBookInputStream(args);
/* 356:    */     
/* 357:    */ 
/* 358:    */ 
/* 359:    */ 
/* 360:338 */     SwingUtilities.invokeLater(new Runnable()
/* 361:    */     {
/* 362:    */       public void run()
/* 363:    */       {
/* 364:340 */         new Viewer(this.val$bookStream);
/* 365:    */       }
/* 366:    */     });
/* 367:    */   }
/* 368:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.viewer.Viewer
 * JD-Core Version:    0.7.0.1
 */